|
|
|
|
|
|
|
The first example shows how the structure is stored internally, with the 2 bytes of padding caused by the alignment and the strings in Unicode. The length of the structure as it is stored internally can be determined using the LenB function. The second example shows how the structure is stored in memory when passed as a DLL call. The string is converted to ANSI, but natural alignment rules still apply. The lower example shows how the structure would be stored in a file if written using the VB Put function. As you can see, all of the fields are packed together with no padding (single-byte alignment). The length of the structure with single-byte alignment can be determined using the Len function. How can you determine the length of a structure when passed as a DLL parameter? You have to figure it out for yourselfthere is no function available to perform the calculation for you. |
|
|
|
|
|
|
|
|
What if a structure used by a DLL function requires single-byte alignment? This is actually the default for virtually all Win32 API structures, so you might think this poses a serious problem. Fortunately, the programmers who developed |
|
|
|
|
|
|
|
|
Figure T7-2:
The struct2 structure in three forms |
|
|
|
|
|
|
|
|
Windows were usually careful to define structures so that they are laid out identically in memory with both single and natural alignment. That's why you'll occasionally see reserved bytes in Win32 API structures. They are, in effect, adding the padding bytes explicitly. |
|
|
|
|
|
|
|
|
For example, you could do this for the struct2 structure by defining it as follows: |
|
|
|
|
|